//
//  UserAccountViewModel.m
//  weibo110
//
//  Created by 臧松 on 15/12/28.
//  Copyright © 2015年 臧松. All rights reserved.
//

#import "UserAccountViewModel.h"
#import "NetworkTools.h"
@interface UserAccountViewModel ()
@property (nonatomic,readonly,getter=isExpired) BOOL expired;
// 用户账户保存路径
@property (nonatomic,readonly) NSString * accountPath;
@end
@implementation UserAccountViewModel
// 对象只要是被 static 修饰就会放在常量区
static id instance;
// 用户账户模型单例
+(instancetype)shareUserAccount{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [[self alloc]init];

    });
    return instance;
}
// 用户头像 url
-(NSURL *)avatar{
    
    return [NSURL URLWithString:self.account.avatar_large];
}
// 初始化方法
-(instancetype)init{
    if (self = [super init]) {
        // 归档路径
        NSString * path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
        path = [path stringByAppendingPathComponent:@"account.plist"];
        // 解档
        self.account = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        
        if (self.expired) {
            self.account = nil;
        }
    }
    return self;
}
// 判断账户是否过期
-(BOOL)isExpired{
    // 大于当前时间没有过期
    if ([self.account.expieresDate compare:[NSDate date]] == NSOrderedAscending) {
        
        return NO;
    }
    return YES;
}
-(NSString *)accessToken{
    if (!self.isExpired) {
        return self.account.access_token;
    }
    return nil;
}

-(BOOL)isUserLogon{
    
    if (self.account.access_token != nil && !self.expired ) {
        
        return YES;
    }
    return NO;
}
//用户账户保存路径
-(NSString *)accountPath{
    
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    return [path stringByAppendingPathComponent:@"account.plist"];
}

-(void)loadAccountTokenCode:(NSString *)code finished:(void (^)(BOOL))finished{
    
    [[NetworkTools sharedTools] loadAccessToken:code finished:^(id result, NSError *error) {
        if (error != nil) {
            NSLog(@"出错了");
            finished(NO);
            return ;
        }
        
        self.account = [UserAccount accountWithDict:result];
        // 加载用户信息
        [self loadUserInfoAccount:self.account finished:finished];
        
    }];

}
-(void)loadUserInfoAccount:(UserAccount*)account finished:(void (^)(BOOL isSuccessed))finished{
    
    [[NetworkTools sharedTools] loadUserInfoUid:account.uid accessToken:account.access_token finished:^(id result, NSError *error) {
        if (error != nil) {
            
            NSLog(@"加载用户信息出错");
            finished(NO);
            return ;
        }
        
        
        
        if (result != nil && [result isKindOfClass:[NSDictionary class]]) {
            
            account.screen_name = result[@"screen_name"];
            account.avatar_large = result[@"avatar_large"];
            account.expieresDate = [NSDate dateWithTimeIntervalSinceNow:[result[@"expires_in"] doubleValue]];
            
            // 保存对象
            [NSKeyedArchiver archiveRootObject:account toFile: self.accountPath];
            finished(YES);
            
        }else {
            NSLog(@"格式出错");
            finished(NO);
            return;
        }
        
    }];
}

@end
